All files / src/app/admin/monitoring/slo/[name] page.tsx

0% Statements 0/245
100% Branches 0/0
0% Functions 0/1
0% Lines 0/245

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
export const dynamic = 'force-dynamic';

import Link from 'next/link';
import { notFound } from 'next/navigation';
import {
  getSLOStatusColor,
  getSLOStatusText,
  formatWindow,
  sloDefinitions,
} from '@/lib/observability';
import { getSLOHistory, getRecentTrend, getTrendIndicator } from '@/lib/observability/slo-history';
import { SLOTrend } from '@/components/features/admin/monitoring/SLOTrend';
import { SLOCard } from '@/components/features/admin/monitoring/SLOCard';

interface Props {
  params: Promise<{ name: string }>;
  searchParams: Promise<{ period?: string }>;
}

/**
 * SLO Detail Page
 *
 * Shows detailed historical data for a single SLO.
 */
export default async function SLODetailPage({ params, searchParams }: Props) {
  const { name } = await params;
  const { period = '7d' } = await searchParams;
  const decodedName = decodeURIComponent(name);

  // Find the SLO definition
  const sloDef = sloDefinitions.find((s) => s.name === decodedName);
  if (!sloDef) {
    notFound();
  }

  // Calculate date range based on period
  const now = new Date();
  const endDate = now;
  let startDate: Date;
  let periodLabel: string;

  switch (period) {
    case '24h':
      startDate = new Date(now.getTime() - 24 * 60 * 60 * 1000);
      periodLabel = 'Last 24 Hours';
      break;
    case '7d':
      startDate = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
      periodLabel = 'Last 7 Days';
      break;
    case '30d':
      startDate = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
      periodLabel = 'Last 30 Days';
      break;
    default:
      startDate = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
      periodLabel = 'Last 7 Days';
  }

  // Fetch historical data
  const [history, sparklineData, trendIndicator] = await Promise.all([
    getSLOHistory(decodedName, startDate, endDate, period === '30d' ? 'daily' : 'hourly'),
    getRecentTrend(decodedName, 24),
    getTrendIndicator(decodedName),
  ]);

  if (!history) {
    notFound();
  }

  // Get current value (most recent data point or use the last sparkline value)
  const currentValue = sparklineData.length > 0
    ? sparklineData[sparklineData.length - 1].value
    : history.summary.avgValue;

  // Determine current status
  const currentStatus = currentValue >= sloDef.target
    ? 'healthy'
    : currentValue >= sloDef.target * 0.99
      ? 'warning'
      : 'critical';

  // Calculate budget consumed
  const errorBudget = 100 - sloDef.target;
  const actualError = 100 - currentValue;
  const budgetConsumed = Math.min(100, (actualError / errorBudget) * 100);

  return (
    <div className="p-6 min-h-screen bg-gray-50 dark:bg-gray-900">
      <div className="max-w-7xl mx-auto">
        {/* Header */}
        <div className="flex items-center justify-between mb-6">
          <div>
            <div className="flex items-center gap-4">
              <Link
                href="/admin/monitoring/slo"
                className="text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300"
              >
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 19l-7-7 7-7" />
                </svg>
              </Link>
              <div>
                <h1 className="text-2xl font-bold text-gray-900 dark:text-white">
                  {decodedName}
                </h1>
                <p className="text-gray-600 dark:text-gray-400">
                  {sloDef.category.charAt(0).toUpperCase() + sloDef.category.slice(1)} SLO | {formatWindow(sloDef.window)} Window
                </p>
              </div>
            </div>
          </div>

          {/* Period Selector */}
          <div className="flex gap-2">
            {[
              { value: '24h', label: '24 Hours' },
              { value: '7d', label: '7 Days' },
              { value: '30d', label: '30 Days' },
            ].map((p) => (
              <Link
                key={p.value}
                href={`/admin/monitoring/slo/${encodeURIComponent(decodedName)}?period=${p.value}`}
                className={`px-4 py-2 text-sm font-medium rounded-md transition-colors ${
                  period === p.value
                    ? 'bg-blue-600 text-white'
                    : 'bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-600'
                }`}
              >
                {p.label}
              </Link>
            ))}
          </div>
        </div>

        {/* Current Status Card */}
        <div className="mb-6">
          <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-3">
            Current Status
          </h2>
          <div className="max-w-md">
            <SLOCard
              name={decodedName}
              current={currentValue}
              target={sloDef.target}
              budgetConsumed={budgetConsumed}
              status={currentStatus}
              window={formatWindow(sloDef.window)}
              statusColor={getSLOStatusColor(currentStatus)}
              statusText={getSLOStatusText(currentStatus)}
              sparklineData={sparklineData.map((t) => t.value)}
              trendIndicator={trendIndicator}
            />
          </div>
        </div>

        {/* Historical Trend Chart */}
        <div className="mb-6">
          <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-3">
            Historical Trend
          </h2>
          <SLOTrend
            sloName={decodedName}
            target={sloDef.target}
            data={history.snapshots}
            summary={history.summary}
            periodLabel={periodLabel}
            height={400}
          />
        </div>

        {/* Detailed Statistics */}
        <div className="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
            Performance Statistics
          </h2>
          <div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-4">
            <div className="p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
              <p className="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide">Target</p>
              <p className="text-2xl font-bold text-gray-900 dark:text-white">{sloDef.target}%</p>
            </div>
            <div className="p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
              <p className="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide">Average</p>
              <p className={`text-2xl font-bold ${
                history.summary.avgValue >= sloDef.target
                  ? 'text-green-600 dark:text-green-400'
                  : 'text-red-600 dark:text-red-400'
              }`}>
                {history.summary.avgValue.toFixed(2)}%
              </p>
            </div>
            <div className="p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
              <p className="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide">Minimum</p>
              <p className={`text-2xl font-bold ${
                history.summary.minValue >= sloDef.target
                  ? 'text-green-600 dark:text-green-400'
                  : 'text-red-600 dark:text-red-400'
              }`}>
                {history.summary.minValue.toFixed(2)}%
              </p>
            </div>
            <div className="p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
              <p className="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide">Maximum</p>
              <p className="text-2xl font-bold text-green-600 dark:text-green-400">
                {history.summary.maxValue.toFixed(2)}%
              </p>
            </div>
            <div className="p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
              <p className="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide">Breaches</p>
              <p className={`text-2xl font-bold ${
                history.summary.breachCount === 0
                  ? 'text-green-600 dark:text-green-400'
                  : 'text-red-600 dark:text-red-400'
              }`}>
                {history.summary.breachCount}
              </p>
            </div>
            <div className="p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg">
              <p className="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide">SLO Compliance</p>
              <p className={`text-2xl font-bold ${
                history.summary.uptimePercentage >= 99
                  ? 'text-green-600 dark:text-green-400'
                  : history.summary.uptimePercentage >= 95
                    ? 'text-yellow-600 dark:text-yellow-400'
                    : 'text-red-600 dark:text-red-400'
              }`}>
                {history.summary.uptimePercentage.toFixed(1)}%
              </p>
            </div>
          </div>

          <div className="mt-6 p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg">
            <h3 className="text-sm font-medium text-blue-800 dark:text-blue-300 mb-2">
              Data Points
            </h3>
            <p className="text-sm text-blue-700 dark:text-blue-400">
              Based on {history.summary.dataPointCount} measurements over the selected period.
              {period === '30d' && ' Data is aggregated daily for 30-day view.'}
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}